//
//  FavCourseList.m
//  Courses
//
//  Created by xxx on 15/1/24.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "FavCourseList.h"

@implementation FavCourseList

- (id)initWithJson:(id)jsonArray
{
    if (self = [super init])
    {
        if (![jsonArray isKindOfClass:[NSDictionary class]])
            return self;
        
//        if ([jsonArray count] != 2)
//            return self;
//        
//        self.markDate = jsonArray[0][@"markDate"];
////        NSLog(@"我的最爱的markDate%@",self.markDate);
//        if ([jsonArray[1][@"courses"] isKindOfClass:[NSArray class]])
//        {
//            NSMutableArray *favCourses = [[NSMutableArray alloc] init];
//            for (id courseJson in jsonArray[1][@"courses"])
//            {
//                if ([courseJson isKindOfClass:[NSDictionary class]])
//                {
//                    PublicCourse *course = [[PublicCourse alloc] initWithJsonDict:courseJson];
//                    [favCourses addObject:course];
//                }
//            }
//            self.favCourses = [favCourses copy];
//        }
        
        
        self.markDate = jsonArray[@"markDate"];
        if ([jsonArray[@"courses"] isKindOfClass:[NSArray class]]) {
            NSMutableArray* favCourses = [[NSMutableArray alloc] init];
            for (id course in jsonArray[@"courses"]) {
                if ([course isKindOfClass:[NSDictionary class]]) {
                    PublicCourse* pubCourse = [[PublicCourse alloc] initWithJsonDict:course];
                    [favCourses addObject:pubCourse];
                }
            }
            self.favCourses = [favCourses copy];
            NSLog(@"%@",self.markDate);
        }
    }
    return self;
}



+ (NSArray *)favCourseLists
{
    NSMutableArray *favCourses = [[NSMutableArray alloc] init];
    Request *request = [Request requestWithName:@"my_favorites"];
    Response *response = [request response];
    if (response.result)
    {
        if ([response.content[@"favorites"] isKindOfClass:[NSArray class]])
        {
            for (id jsonArray in response.content[@"favorites"])
            {
                FavCourseList *courseList = [[FavCourseList alloc] initWithJson:jsonArray];
                [favCourses addObject:courseList];
            }
        }
    }
    
    return [favCourses copy];
}

@end
